﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Lima.ResumeSys.Util
{
    public class SysUtil
    {
        /// <summary>
        /// 图片并转换为字节数组
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        public byte[] ImageToByteArray(Image image)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                image.Save(ms, image.RawFormat);
                return ms.ToArray();
            }
        }
        public string CreateFilePerNo(string id)
        {
            string upFileFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "download");

            string targetFolder = Path.Combine(upFileFolder, id);

            if (!Directory.Exists(upFileFolder))
            {
                Directory.CreateDirectory(upFileFolder);
            }

            // 检查文件夹是否存在，不存在则创建
            if (!Directory.Exists(targetFolder))
            {
                Directory.CreateDirectory(targetFolder);

            }

            return targetFolder;
        }
        /// <summary>
        /// 文件并转换为字节数组
        /// </summary>
        /// <returns></returns>
        public byte[] FileToByteArray()
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    string filePath = openFileDialog.FileName;
                    return File.ReadAllBytes(filePath);
                }
            }
            return null;
        }

        /// <summary>
        /// 直接通过文件路径转换
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public byte[] FileToByteArray(string filePath)
        {
            return File.ReadAllBytes(filePath);
        }
        public Image ByteArrayToImage(byte[] byteArray)
        {
            using (MemoryStream ms = new MemoryStream(byteArray))
            {
                return Image.FromStream(ms);
            }
        }
        /// <summary>
        /// 身份证号验证
        /// </summary>
        /// <param name="idCard"></param>
        /// <returns></returns>
        public  bool IsIdCardChecksumValid(string idCard)
        {
            if (idCard.Length != 18)
                return false;

            // 权重系数（第1到17位）
            int[] weights = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
            // 校验码对照表（第18位）
            char[] checkCodes = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };

            int sum = 0;
            for (int i = 0; i < 17; i++)
            {
                sum += (idCard[i] - '0') * weights[i];
            }

            int mod = sum % 11;
            char correctCheckCode = checkCodes[mod];

            return idCard[17] == correctCheckCode;
        }
    }
}
